/* eslint-disable no-unused-vars */
import Vue from 'vue'
import App from './App.vue'
import { Login, Layout, DataList,BaiduMap,Analysis,BaseForm } from './pages'
import CachePages from './components/CachePages'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const Home = {
  name: "admin-home",
  template: '<div>Home</div>'
}

const routes = [
  { path: '/', component: Home },
  { path: '/login', component: Login },
  {
    path: '/admin', component: Layout,
    children: [
      {
        // 当 /user/:id/profile 匹配成功，
        // UserProfile 会被渲染在 User 的 <router-view> 中
        icon: 'home',
        path: 'home',
        component: Home,
        name: "首页",
        powerkey: "bbbbbb",
      },
      {
        icon: 'car',
        path: 'Dashboard',
        component: CachePages,
        name: "dashboard",
        redirect:"/admin/dashboard/analysis",
        children: [
          {
            icon: 'shower',
            path: 'analysis',
            component: Analysis,
            name: "分析页",
            powerkey: "bbbbbb",
          },
          {
            path: 'monitor',
            component: DataList,
            name: "监控页",
            powerkey: "bbbbbb",
          },
          {
            path: 'workplace',
            component: Login,
            name: "工作台",
            powerkey: "bbbbbb",
          },
        ]
      },
      {
        path: 'resource',
        icon: 'walking',
        component: CachePages,
        name: "资源",
        redirect:"/admin/resource/baidu-map",
        children: [
          {
            path: 'baidu-map',
            component: BaiduMap,
            name: "百度地图",
          },
        ]
      },
      {
        path: 'form',
        icon:'el-icon-view',
        component: CachePages,
        name: "表单页",
        children: [
          {
            path: 'base-form',
            component: BaseForm,
            name: "基础表单",
            powerkey: "cccccc"
          },
        ]
      },

      {
        path: 'list',
        component: DataList,
        name: "列表页",
        powerkey: "aaaaa"
      },
    ]
  },
];
let route_list = [];
function getRouteList(list, p) {
  list.forEach(r => {
    let path = r.path;
    if (p) {
      path = (p.fullpath || p.path) + '/' + r.path;
    }
    r.fullpath = path;
    route_list.push({
      fullpath: path,
      path: r.path,
      name: r.name,
      closable: true,
      component_name: r.component.name,
    });
    if (r.children && r.children.length > 0) {
      getRouteList(r.children, r);
    }
  });
}
getRouteList(routes, null);

let router = new VueRouter({
  routes // (缩写) 相当于 routes: routes
});
router.afterEach((to, from) => {
  let store = router.app.$store;
  let win_list = store.state.global.open_windows;
  let r = route_list.find(x => x.fullpath == to.path);

  let w = win_list.find(x => x.fullpath == to.path);
  if (r) {
    if (!w) {
      if (r.path != "home") {
        win_list.push(r);
        store.commit('global/setOpenWindows', win_list);
      }
    }
    store.commit('global/setActiveWindow', r.fullpath);
  }
});
export default router;
